package com.feiyutech.camera2demo

import android.Manifest
import android.content.Context
import android.content.Intent
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import cn.zfs.common.entity.PermissionsRequester
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var permissionsRequester: PermissionsRequester? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        permissionsRequester = PermissionsRequester(this)
        val list = arrayListOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        permissionsRequester?.setOnRequestResultListener {  }
        permissionsRequester?.check(list)
        val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager?
        val cameraCharacteristics = cameraManager?.getCameraCharacteristics("0")
        tvSupportLevel.text = "支持级别：${getSupportLevelString(cameraCharacteristics!!.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL))}"
        initEvents()
    }

    private fun initEvents() {
        btnFlash.setOnClickListener { startActivity(Intent(this, FlashActivity::class.java)) }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        permissionsRequester?.onActivityResult(requestCode, resultCode, data)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        permissionsRequester?.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }
    
    private fun getSupportLevelString(level: Int): String {
        return when (level) {
            CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED -> "LIMITED"
            CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL -> "FULL"
            CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3 -> "LEVEL_3"
            else -> "LEGACY"
        }
    }
}
